use sea_orm::ConnectOptions;
use serde::Deserialize;

fn default_url() -> String {
    "sqlite://sqlite.db".to_string()
}

#[derive(Debug, Clone, Deserialize)]
pub struct DbConfig {
    #[serde(default = "default_url")]
    pub url: String,
    #[serde(default)]
    pub max_connections: Option<u32>,
    #[serde(default)]
    pub min_connections: Option<u32>,
}

impl Default for DbConfig {
    fn default() -> Self {
        Self {
            url: default_url(),
            max_connections: None,
            min_connections: None,
        }
    }
}

impl DbConfig {
    pub fn get_connect_opt(&self) -> ConnectOptions {
        let mut opt = ConnectOptions::new(self.url.clone());
        if let Some(num) = self.max_connections {
            opt.max_connections(num);
        }
        if let Some(num) = self.min_connections {
            opt.min_connections(num);
        }
        opt
    }

    pub fn url(&self) -> &str {
        &self.url
    }
}
